package com.yjz.ui.banner.indicator;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.yjz.ui.R;
import com.yjz.ui.util.DisplayUtil;

/**
 * 作用 : 圆形指示器
 * 作者 : yjz
 * 时间 : 2024/10/21 15:23
 */
public class CircleIndicator extends FrameLayout implements BannerIndicator<FrameLayout> {

    private final int mPointLeftRightPadding;
    private final int mPointTopBottomPadding;
    /**
     * 正常状态下的指示点
     */
    @DrawableRes
    private final int mPointNormal = R.drawable.shape_point_normal;

    /**
     * 选中状态下的指示点
     */
    @DrawableRes
    private final int mPointSelected = R.drawable.shape_point_select;


    public CircleIndicator(@NonNull Context context) {
        this(context, null);
    }

    public CircleIndicator(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CircleIndicator(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mPointLeftRightPadding = DisplayUtil.dp2px(5f, context.getResources());
        mPointTopBottomPadding = DisplayUtil.dp2px(15f, context.getResources());
    }

    @Override
    public FrameLayout get() {
        return this;
    }

    @Override
    public void onInflate(int count) {
        removeAllViews();
        if (count <= 0) {
            return;
        }
        LinearLayout groupView = new LinearLayout(getContext());
        groupView.setOrientation(LinearLayout.HORIZONTAL);
        ImageView imageView;
        int VWC = ViewGroup.LayoutParams.WRAP_CONTENT;
        LinearLayout.LayoutParams imageViewParams = new LinearLayout.LayoutParams(VWC, VWC);
        imageViewParams.gravity = Gravity.CENTER_VERTICAL;
        imageViewParams.setMargins(
                mPointLeftRightPadding,
                mPointTopBottomPadding,
                mPointLeftRightPadding,
                mPointTopBottomPadding
        );
        for (int i = 0; i < count; i++) {
            imageView = new ImageView(getContext());
            imageView.setLayoutParams(imageViewParams);
            if (i == 0) {
                imageView.setImageResource(mPointSelected);
            } else {
                imageView.setImageResource(mPointNormal);
            }
            groupView.addView(imageView);
        }
        LayoutParams groupViewParams = new LayoutParams(VWC, VWC);
        groupViewParams.gravity = Gravity.CENTER | Gravity.BOTTOM;
        addView(groupView, groupViewParams);
    }

    @Override
    public void onPointChange(int current, int count) {
        ViewGroup viewGroup = (ViewGroup) getChildAt(0);

        for (int i = 0; i < viewGroup.getChildCount(); i++) {
            ImageView imageView = (ImageView) viewGroup.getChildAt(i);
            if (i == current) {
                imageView.setImageResource(mPointSelected);
            } else {
                imageView.setImageResource(mPointNormal);
            }
            imageView.requestLayout();
        }
    }
}
